package edu.ufl.android.poker;

import java.util.LinkedList;

public class Player {

	private LinkedList<Card> holeCards;
	private LinkedList<Hand> currentHands;
	private Hand bestHand;
	private int currentStack;
	private boolean playingThisRound;
	private boolean handUpdated;
	
	public Player()
	{
		currentStack = 0;
		holeCards = new LinkedList<Card>();
		currentHands = new LinkedList<Hand>();
		playingThisRound = true;
		handUpdated = false;
	}
	private Hand calcBestHand()
	{
		Hand temp = currentHands.get(0);
		for(int i=1; i<currentHands.size(); i++)
		{
			if (temp.compareTo(currentHands.get(i)) < 0)
			{
				temp = currentHands.get(i);
			}
		}
		return temp;
	}
	public Hand getCurrentBestHand()
	{
		if(handUpdated || bestHand == null)
		{
			handUpdated = false;
			return calcBestHand();
		}
		else
		{
			return bestHand;
		}
	}
	public void calcPossibleHands(LinkedList<Card> commCards)
	{
		if(commCards.size() < 3)
		{
			return;
		}
		Card[] temp = new Card[5];
		for(int i=0; i<2; i++)
		{
			temp[i] = holeCards.get(0);
		}
		if(commCards.size() <= 3)
		{			
			for(int i=2; i<5; i++)
			{
				temp[i] = commCards.get(i-2);
			}
			currentHands.add(new Hand(temp));
		}
		else if(commCards.size() <= 4)
		{
			temp[2] = commCards.get(0);
			temp[3] = commCards.get(1);
			temp[4] = commCards.get(3);
			currentHands.add(new Hand(temp));
			
			temp[2] = commCards.get(1);
			temp[3] = commCards.get(2);
			temp[4] = commCards.get(3);
			currentHands.add(new Hand(temp));
			
			temp[2] = commCards.get(0);
			temp[3] = commCards.get(2);
			temp[4] = commCards.get(3);
			currentHands.add(new Hand(temp));
		}
		else if(commCards.size() <= 5)
		{
			temp[2] = commCards.get(0);
			temp[3] = commCards.get(1);
			temp[4] = commCards.get(4);
			currentHands.add(new Hand(temp));
			
			temp[2] = commCards.get(1);
			temp[3] = commCards.get(2);
			temp[4] = commCards.get(4);
			currentHands.add(new Hand(temp));
			
			temp[2] = commCards.get(0);
			temp[3] = commCards.get(3);
			temp[4] = commCards.get(4);
			currentHands.add(new Hand(temp));
			
			temp[2] = commCards.get(1);
			temp[3] = commCards.get(2);
			temp[4] = commCards.get(4);
			currentHands.add(new Hand(temp));
			
			temp[2] = commCards.get(1);
			temp[3] = commCards.get(3);
			temp[4] = commCards.get(4);
			currentHands.add(new Hand(temp));
			
			temp[2] = commCards.get(2);
			temp[3] = commCards.get(3);
			temp[4] = commCards.get(4);
			currentHands.add(new Hand(temp));
		}
	}
	public LinkedList<Hand> getCurrentHands()
	{
		return currentHands;
	}
	public LinkedList<Card> getHoleCards()
	{
		return holeCards;
	}
	public int getStack()
	{
		return currentStack;
	}
	public boolean hasEnough(int amount)
	{
		return amount <= currentStack;
	}
	public void modifyStack(int amount)
	{
		currentStack += amount;
	}
	public boolean playingThisRound()
	{
		return playingThisRound;
	}
	public void receiveCard(Card c)
	{
		holeCards.add(c);
		handUpdated = true;
	}
	public String toStringHoleCards()
	{
		String toReturn = holeCards.get(0).toString();
		toReturn += " \n";
		toReturn += holeCards.get(1).toString();
		toReturn += " \n";
		return toReturn;
	}
	
	
}
